package com.naiterui.ehp.bs.wechat.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bs.wechat.config.WeChatConfig;
import com.naiterui.ehp.bs.wechat.properties.WeChatProperties;
import com.naiterui.ehp.bs.wechat.service.IWeChatService;
import com.naiterui.ehp.bs.wechat.utils.SHA1;

/**
 * @author zhoupeipei
 * @date 2019-06-28 11:29
 * @since 1.0.0
 */
@Service
public class WeChatServiceImpl implements IWeChatService {
    private static final Logger LOGGER = LoggerFactory.getLogger(WeChatServiceImpl.class);
    @Autowired
    WeChatConfig weChatConfig;
    @Override
    public String checkToken(String signature, String timestamp, String nonce, String echostr) {
        WeChatProperties paConfig = weChatConfig.paProperties();
        String token = paConfig.getToken();
        String configSign = SHA1.gen(token, timestamp, nonce);
        if (configSign.equals(signature)) {
            LOGGER.info("token校验通过，signature：{}；timestamp：{}；nonce：{}；echostr：{}；", signature, timestamp, nonce, echostr);
            return echostr;
        }
        LOGGER.info("token校验失败，signature：{}；timestamp：{}；nonce：{}；echostr：{}；", signature, timestamp, nonce, echostr);
        return "FILED";
    }
}
